package com.stylefeng.guns.modular.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitConsignerID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.ConsignerMapper;
import com.stylefeng.guns.modular.system.model.Consignee;
import com.stylefeng.guns.modular.system.model.Consigner;
import com.stylefeng.guns.modular.system.service.IConsignerService;

/**
 * 发货人信息控制器
 *
 * @author fengshuonan
 * @Date 2018-04-02 22:10:18
 */
@Controller
@RequestMapping("/consigner")
public class ConsignerController extends BaseController {

    private String PREFIX = "/system/consigner/";

    @Autowired
    private IConsignerService consignerService;
    @Autowired
    private ConsignerMapper consignerMapper;

    /**
     * 跳转到发货人信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "consigner.html";
    }

    /**
     * 跳转到添加发货人信息
     */
    @RequestMapping("/consigner_add")
    public String consignerAdd() {
        return PREFIX + "consigner_add.html";
    }

    /**
     * 跳转到修改发货人信息
     */
    @RequestMapping("/consigner_update/{consignerId}")
    public String consignerUpdate(@PathVariable Integer consignerId, Model model) {
        Consigner consigner = consignerService.selectById(consignerId);
        model.addAttribute("item",consigner);
        LogObjectHolder.me().set(consigner);
        return PREFIX + "consigner_edit.html";
    }

    /**
     * 获取发货人信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Consigner> page = new PageFactory<Consigner>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(consignerMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Consigner> entityWrapper = new EntityWrapper<>();
    		Wrapper<Consigner> wrapper = entityWrapper.like("consigner_name", condition);
    		page.setRecords(consignerMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }

    /**
     * 新增发货人信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Consigner consigner) {
    	consigner.setConsignerId(InitConsignerID.getInstance().initDisorderID());
        consignerService.insert(consigner);
        return SUCCESS_TIP;
    }

    /**
     * 删除发货人信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer consignerId) {
        consignerService.deleteById(consignerId);
        return SUCCESS_TIP;
    }
    
    /**
     * 通过name，模糊查询所有发货人信息
     */
    @RequestMapping(value="/listbyname")
    @ResponseBody
    public Object listByName(@RequestParam String consigner){
    	EntityWrapper<Consigner> entityWrapper = new EntityWrapper<>();
    	Wrapper<Consigner> wrapper = entityWrapper.like("consigner_name", consigner).last("limit 20");
    	return consignerService.selectList(wrapper);
    }

    /**
     * 修改发货人信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Consigner consigner) {
        consignerService.updateById(consigner);
        return SUCCESS_TIP;
    }

    /**
     * 发货人信息详情
     */
    @RequestMapping(value = "/detail/{consignerId}")
    @ResponseBody
    public Object detail(@PathVariable("consignerId") Integer consignerId) {
        return consignerService.selectById(consignerId);
    }
}
